<template>
	<div class="finish-create">
		<div class="header">您选择的项目'{{ createProjectData.project.projectname }}'已经完成创建,请选择项目截至日期,并提交</div>
		<el-calendar v-model="value"></el-calendar>
		<el-button size="med" type="primary" @click="submitProject">{{ createProjectData._id ? "确认修改" : "确认完成" }}</el-button>
	</div>
</template>

<script>
	import {mapState} from "vuex"
	export default {
		data() {
			return {
				value:new Date()
			}
		},
		computed:{
			...mapState(['createProjectData','relevanceUserID'])
		},
		created() {
			if(this.createProjectData._id){ //id存在的情况下为修改
				this.value = new Date(this.createProjectData.vaildDate);
			}
		},
		methods: {
			submitProject(){
				let {createProjectData,value,relevanceUserID} = this;
				let vaildDate = new Date(value).getTime()
				createProjectData.vaildDate = vaildDate;
				
				// 非修改状态下增加
				if(!createProjectData._id){
					createProjectData.relevanceUserID = relevanceUserID;
					//队友增加模块记录字段
					createProjectData.partners.map(item=>{
						item.finishProjectDoc = {
							fileName:"",
							fileUrl:"",
							time:Date.now()
						};
						
						return item;
					})
					//增加队友是否上传文档字段(数组存储队友ID)
					createProjectData.partnerChangeUploadModule = [];
				}
				
				if(vaildDate <= (new Date().getTime())){
					this.$message({
						message: '请确认您的时间是否选择正确!',
						type: 'warning',
						duration:2000
					});
					return;
				}
				
				// 通过绑定的小程序用户ID进行唯一存储
				this.$alert(`确认完成${!createProjectData._id ? '创建' : '修改'}吗?`, '提示', {
				   confirmButtonText: '确认提交',
				   showCancelButton:true,
				   cancelButtonText:"再次检查",
				   type:"warning",
				   callback: async (action) => {
					   if(action === "confirm"){
						  if(!createProjectData._id){ // 创建
							let res = await this.$network({
								api:"finishCreateProject",
								data:createProjectData
							})
							  
							if(res.status === 2){
							  	this.$message({
							  		message: '当前项目已经完成创建,请勿重复创建!',
							  		type: 'warning',
							  		duration:2000
							  	});
							}
							
							if(res.status === 0){
							   this.$store.state.createProjectData = {}
								this.$alert('您项目已经完成创建,记得提示队友及时完成哦!', '提示', {
									confirmButtonText: '知道了',
									type:"success",
									showClose:false,
									callback:action => {
										if(action === "confirm"){
											this.$store.state.createProjectData = {};
											this.$router.push("/project_admin/underway")
											uni.setStorageSync('curAsideMenuItem',"2-2") //缓存当前asideBar
											uni.setStorageSync('curRouter',"/project_admin/underway") // 缓存当前路由
										}
									}
								});
							}
							
						  } else {  //修改
							let updateRes = await this.$network({
								api:"updateProject",
								data:createProjectData
							})
							if(updateRes.status === 0){
								this.$store.state.createProjectData = {};
								this.$router.push("/project_admin/underway")
								this.$message({
									message: '修改成功!',
									type: 'success',
									duration:2000
								});
							}
						  }
					   }
				   }
				});
			}
		}
	}
</script>

<style lang="scss">
	.finish-create{
		text-align: center;
		.el-button{
			margin-top: 40px;
		}
		.header{
			margin-top: 40px;
			color: $uni-color-title;
			font-size: $uni-font-size-extra-large;
		}
		
	}
</style>
